package com.yami.shop.api.controller;

import com.yami.shop.bean.app.dto.CouponDto;
import com.yami.shop.bean.model.Coupon;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.CouponService;
import com.yami.shop.service.ShopDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/coupon/coupon")
public class CouponController {

    @Autowired
    private CouponService couponService;

    @Autowired
    private ShopDetailService shopDetailService;

    /**
     * 获取单品优惠券信息
     */
    @GetMapping("/info")
    @PreAuthorize("@pms.hasPermission('coupon:coupon:info')")
    public ResponseEntity<List<CouponDto>> info(@Valid Long shopId,@Valid Long prodId){
        String userId = SecurityUtils.getUser().getUserId();
        Integer couponKind = 0;
        List<CouponDto> couponList = couponService.getCouponByShopIdAndprodId(shopId,userId,couponKind,prodId);
        // 店铺图片
        return ResponseEntity.ok(couponList);
    }

    /**
     * 获取店铺优惠券信息
     */
    @GetMapping("/shopCoupon/{shopId}")
    @PreAuthorize("@pms.hasPermission('coupon:coupon:shopCoupon')")
    public ResponseEntity<List<CouponDto>> shopCoupon(@PathVariable("shopId") Long shopId){
        String userId = SecurityUtils.getUser().getUserId();
        Integer couponKind = 1;
        List<CouponDto> couponList = couponService.getCouponByShopId(shopId,userId,couponKind);
        // 店铺图片
        return ResponseEntity.ok(couponList);
    }

    /**
     * 获取平台优惠券信息
     */
    @GetMapping("/platFormCoupon")
    @PreAuthorize("@pms.hasPermission('coupon:coupon:platFormCoupon')")
    public ResponseEntity<List<CouponDto>> platFormCoupon(){
        Long shopId = null;
        String userId = SecurityUtils.getUser().getUserId();
        Integer couponKind = 2;
        List<CouponDto> couponList = couponService.getCouponByShopId(shopId,userId,couponKind);
        // 店铺图片
        return ResponseEntity.ok(couponList);
    }

    /**
     * 获取用户个人优惠券,平台优惠券
     */
    @GetMapping("/getCoupon")
    public ResponseEntity<List<CouponDto>> getCoupon(){
        String userId = SecurityUtils.getUser().getUserId();
        List<CouponDto> couponDtoList = null;
        couponDtoList = couponService.getCoupon(userId);
        if(couponDtoList.size()>0){
            for(CouponDto couponDto:couponDtoList){
                 Long shopId = couponDto.getShopId();
                 ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(shopId);
                 String shopName = shopDetail.getShopName();
                 couponDto.setShopName(shopName);
            }
        }
        return ResponseEntity.ok(couponDtoList);
    }



}
